home *** CD-ROM | disk | FTP | other *** search
/ Interplay's Learn to Program Basic (Review Copy) / Learn to Program Basic Review Copy (Interplay)(June 23, 1998).ISO / pc / ltpbasic / projects / schpong.bas < prev    next >
Encoding:
BASIC Source File  |  1998-04-07  |  10.1 KB  |  518 lines

  1. rem SchPong!
  2. rem by Scott Mathews
  3. rem September 20, 1997
  4. rem thanks to Brad for:
  5. rem dinner....
  6. rem inspiration....
  7. rem and the bouncing ball routine... :)
  8. rem guess Nolan B deserves some credit
  9. rem in here too.
  10.  
  11.  
  12. cls
  13. inplay = true
  14. again = false
  15. Lbuffer = 0
  16. Rbuffer = 0
  17.  
  18. rem how many points to win?
  19. victory = 1
  20.  
  21. rem beginning player scores
  22. leftscore = 0
  23. rightscore = 0
  24.  
  25. rem keyboard inputs for controlling the paddles
  26. rem the left player
  27. lup$ = "w"
  28. ldown$ = "s"
  29. lspeed$ = "d"
  30. lenglish$ = "a"
  31.  
  32. rem the right player
  33. rup$ = "i"
  34. rdown$ = "k"
  35. rspeed$ = "j"
  36. renglish$ = "l"
  37.  
  38. rem playing field size and color
  39. rem the paddle size and positnio are determined
  40. rem by the size of the playing field
  41. rem try playing on a tiny field!
  42. upperX = 10
  43. lowerX = 310
  44. upperY = 25
  45. lowerY = 230
  46. fieldcolor = 59
  47. linecolor = 94
  48.  
  49. rem paddle size
  50. padwidth = int((lowerX-upperX)/30)
  51. padlength = int((lowerY - UpperY)/4)
  52.  
  53. rem paddle speed 
  54. padspeed = 9
  55.  
  56. rem ball size, color, maximum speed, maximum engligh
  57. ballsize = 5
  58. ballcolor = 3
  59. maxspeed = 25
  60. maxenglish = 15
  61.  
  62. rem paddle direction start values
  63. Lpaddir = 0
  64. Rpaddir = 0
  65.  
  66. rem ball update start values
  67. balllastX = 10
  68. balllastY = 10
  69.  
  70. rem left paddle start position and color
  71. LpadX = int((lowerX - UpperX)/10)
  72. LpadY = int((lowerY - UpperY)/2)
  73. LpadlastY = int((lowerY - UpperY)/2)
  74. lpadcolor = 54
  75.  
  76. rem right paddle start position and color
  77. RpadX = lowerX - LpadX
  78. RpadY = int((lowerY - UpperY)/2)
  79. RpadlastY = int((lowerY - UpperY)/2)
  80. rpadcolor = 150
  81.  
  82. rem sounds
  83. hitSnd = LoadSound("Pop")
  84. bounceSnd = LoadSound("Bounce")
  85. missSnd = LoadSound("GameShow")
  86. serveSnd = LoadSound("Payoff")
  87. endSnd = LoadSound("GamOver1")
  88.  
  89. rem timer delay for start of service
  90. startDelay = 1600
  91.  
  92. rem this color is used to erase the ball and paddles
  93. backColor = pget (0,0)
  94.  
  95. gosub intro
  96. gosub getnames
  97. startover:
  98. gosub ballstart
  99.  
  100. rem the main loop
  101. while inplay
  102. cmd$ = Inkey$
  103.  
  104. rem if you want to quit...
  105. if cmd$ = "q" then inplay = false
  106.  
  107. rem assume no movement
  108. Lpaddir = 0
  109.  
  110. rem move the left paddle up
  111. if keydown(Lup$) then 
  112. Lpaddir = -padspeed
  113. endif
  114.  
  115. rem move the left paddle down
  116. if keydown(LDown$) then
  117. Lpaddir = padspeed
  118. endif
  119.  
  120. rem assume no movement
  121. RpadDir = 0
  122.  
  123. rem move the right paddle up
  124. if keydown(rup$) then
  125. RPaddir = -padspeed
  126. endif
  127.  
  128. rem move right paddle down
  129. if keydown(rdown$) then
  130. RPadDir = padspeed
  131. endif
  132.  
  133. suspendupdate
  134.  
  135. rem draw the player names and scores
  136. textcolor 21
  137. position upperx/8,0
  138. print name1$;"     ";
  139. print leftscore
  140. position ((upperX+lowerX)/2)/8,0
  141. print name2$;"     ";
  142. print rightscore
  143.  
  144. rem draw the playing field
  145. color fieldcolor
  146. rect upperX,upperY to lowerX,lowerY
  147. color linecolor
  148. line (upperX+lowerX)/2,upperY to (upperX+lowerX)/2,lowerY
  149.  
  150. rem draw the ball and its update
  151. rem if ball not in service timer
  152. If ballXDir <> 0 then
  153. color backColor
  154. fillCircle balllastX,balllastY,ballsize
  155. color ballcolor
  156. fillCircle ballx,bally,ballsize
  157. Else
  158. Rem count down start timer
  159. Rem play sound a little before serving
  160. If Timer() - startTimer > startDelay/2 Then
  161. If serveSoundPlayed = FALSE Then
  162. Rem play serving sound
  163. PlaySound(serveSnd)
  164. serveSoundPlayed = TRUE
  165. EndIf
  166. Endif
  167.  
  168. Rem serve ball
  169. If Timer() - startTimer > startDelay Then
  170. ballXdir = serveXdir
  171. EndIf
  172. Endif
  173.  
  174. rem draw left paddle and its update
  175. color backcolor
  176. fillrect LpadX,Lpadlasty to LpadX+padwidth,LpadlastY+padlength
  177. color lpadcolor
  178. fillrect LpadX,LpadY to LpadX+padwidth,LpadY+padlength
  179.  
  180. rem draw right paddle and its update
  181. color backcolor
  182. fillrect RpadX,Rpadlasty to RpadX+padwidth,RpadlastY+padlength
  183. color rpadcolor
  184. fillrect RpadX,RpadY to RpadX+padwidth,RpadY+padlength
  185.  
  186. resumeupdate
  187.  
  188. rem check for collision
  189. rem left paddle
  190. If ballLastX > ballX then
  191. if ballX - ballsize <= Lpadx + padwidth AND ballLastX >= LpadX + padWidth then
  192. if ballY+ballSize > LpadY and bally-ballSize < LpadY + padlength then
  193. ballxdir = -ballxdir
  194. Rem Play hit sound
  195. PlaySound(hitSnd)
  196. Rem see if we are applying speed or english modifiers
  197. If KeyDown(lspeed$) Then ballXDir = ballXDir * 2
  198. If KeyDown(lenglish$) Then ballYDir = ballYDir + Random(-ballxdir,ballxdir)
  199. Rem Limit to maximum
  200. If ballXDir > maxSpeed then ballXDir = maxSpeed
  201. If ballYDir > maxEnglish then ballYDir = maxEnglish
  202. If ballYDir < -maxEnglish then ballYDir = -maxEnglish
  203. endif
  204. endif
  205. EndIf
  206.  
  207. rem right paddle
  208. If ballLastX < ballX then
  209. if ballX + ballsize >= Rpadx AND ballLastX <= Rpadx Then
  210. if ballY+ballSize > RpadY and bally-ballSize < RpadY + padlength then
  211. ballxdir = -ballxdir
  212. Rem Play hit sound
  213. PlaySound(hitSnd)
  214. if keydown("Shift") then ballYDir = 0
  215. Rem see if we are applying speed or english modifiers
  216. If KeyDown(rspeed$) Then ballXDir = ballXDir * 2
  217. If KeyDown(renglish$) Then ballYDir = ballYDir + Random(ballxdir,-ballxdir)
  218. Rem Limit to maximum
  219. If ballXDir < -maxSpeed then ballXDir = -maxSpeed
  220. If ballYDir > maxEnglish then ballYDir = maxEnglish
  221. If ballYDir < -maxEnglish then ballYDir = -maxEnglish
  222. endif
  223. endif
  224. EndIf
  225.  
  226. rem reset the volley for left player
  227. if ballx > ((upperX+lowerX)/2) then
  228. Lspeedup = false
  229. Lenglishup = false
  230. endif
  231.  
  232. rem reset the volley for right player
  233. if ballx < ((upperX+lowerX)/2) then
  234. rspeedup = false
  235. renglishup = false
  236. endif
  237.  
  238. rem know where to place the ball update
  239. balllastX = ballx
  240. balllastY = bally
  241.  
  242. rem don't let the ball go out of 
  243. rem northern or southern bounds
  244. if bally < upperY + ballsize or bally > lowerY - ballsize then 
  245. ballyDir = -ballyDir
  246. Rem play bounce sound
  247. PlaySound(bounceSnd)
  248. endif
  249.  
  250. rem add to the speed of the ball
  251. ballx = ballx + ballxDir
  252.  
  253. rem add to the english of the ball
  254. bally = bally + ballyDir
  255.  
  256. rem know where to place the paddle update
  257. LpadlastY = LpadY
  258. RpadlastY = RpadY
  259.  
  260. rem move the paddles
  261. rem check for boundary with playing field first
  262. rem did the left paddle bonk into the lower boundary?
  263. if LpadY + padlength - Lbuffer > LowerY - padspeed then 
  264. LpadY = LowerY - padlength
  265. LPadDir = 0
  266. Lbuffer = padspeed
  267. else
  268. rem did the left paddle bonk into the upper boundary?
  269. if LpadY + Lbuffer <  UpperY + padspeed then
  270. LpadY = UpperY
  271. LPadDir = 0
  272. Lbuffer = padspeed
  273. else
  274. rem if it didn't bonk into anything, move it
  275. LpadY = LpadY + Lpaddir
  276. Lbuffer = 0
  277. endif
  278. endif
  279.  
  280. rem did the right paddle bonk into the lower boundary?
  281. if RpadY + padlength - Rbuffer  > LowerY - padspeed then 
  282. RpadY = LowerY - padlength
  283. RPadDir = 0
  284. Rbuffer = padspeed
  285. else
  286. rem did the right paddle bonk into the upper boundary?
  287. if RpadY + Rbuffer <  UpperY + padspeed then
  288. RpadY = UpperY
  289. RPadDir = 0
  290. Rbuffer = padspeed
  291. else
  292. rem if it didn't bonk into anything, move it
  293. RpadY = RpadY + Rpaddir
  294. Rbuffer = 0
  295. endif
  296. endif
  297.  
  298. rem if the ball goes past the paddle, score a point
  299. Rem if ballx < LpadX-6 or ballx > RpadX + padwidth+6 then
  300. If ballx < -ballSize*2 or ballx > 320+ballSize*2 Then
  301. ballxdir = 0
  302. Rem play missed ball sound
  303. PlaySound(missSnd)
  304. gosub score
  305. endif
  306.  
  307. wend
  308.  
  309. gosub playagain
  310. if again = true then goto startover
  311.  
  312. end
  313.  
  314. rem ball start up values
  315. ballstart:
  316. color backcolor
  317. fillcircle balllastX,balllastY,ballsize
  318. 'color fieldcolor
  319. 'rect upperX,upperY to lowerX,lowerY
  320. ballx = 160
  321. bally = 120
  322. let service = random(1,2)
  323. if service = 1 then serveXDir = 3
  324. if service = 2 then serveXDir = -3
  325. ballXDir = 0 ' don't move until timer expires
  326. ballYDir = 1
  327. lspeedup = false
  328. rspeedup = false
  329. lenglishup = false
  330. renglishup = false
  331. Rem Set timer to delay before service
  332. startTimer = timer()
  333. serveSoundPlayed = FALSE
  334.  
  335. return
  336.  
  337. rem add to the score
  338. score:
  339.  
  340. if ballx < (upperx+lowerx)/2 then
  341. rightscore = rightscore + 1
  342. endif
  343.  
  344. if ballx > (upperx+lowerx)/2 then 
  345. leftscore = leftscore +1
  346. endif
  347.  
  348. if leftscore = victory then
  349. position upperx/8,0
  350. textcolor 100
  351. print name1$;"     ";
  352. print leftscore
  353. for I = 20 to 200
  354. color I
  355. fillrect LpadX,LpadY to LpadX+padwidth,LpadY+padlength
  356. inplay = false
  357. next I
  358.  
  359. Rem play end of game sound
  360. Sleep 10
  361. PlaySound(endSnd)
  362.  
  363. banner +name1$+" wins!"
  364. goto endgame
  365. endif
  366.  
  367. if rightscore = victory then
  368. position ((upperX+lowerX)/2)/8,0
  369. textcolor 100
  370. print name2$;"     ";
  371. print rightscore
  372. for I = 20 to 200
  373. color I
  374. fillrect RpadX,RpadY to  RpadX+padwidth,RpadY+padlength
  375. inplay = false
  376. next I
  377. banner +name2$+" wins!"
  378. goto endgame
  379. endif
  380.  
  381. gosub ballstart
  382.  
  383. endgame:
  384. return
  385.  
  386. intro:
  387. textcolor 99
  388. position 10,5
  389. print "W ";
  390. sleep 2
  391. print "E ";
  392. sleep 2
  393. print "L ";
  394. sleep 2
  395. print "C ";
  396. sleep 2
  397. print "O ";
  398. sleep 2
  399. print "M ";
  400. sleep 2
  401. print "E ";
  402. print "    ";
  403. sleep 2
  404. print "T ";
  405. sleep 2
  406. print "O";
  407. sleep 10
  408.  
  409. cls
  410. color 21
  411. fillrect 0,0 to 319,239
  412. drawtext "@24,BOLD,color 58,helvetica@S" AT 80,70
  413. drawtext  "@48,BOLD,color 27,courier@C" AT 100,75
  414. drawtext "@16,BOLD,color 220,times@H" AT 135,95
  415. drawtext "@72,BOLD,color 48,courier@P" AT 150,65
  416. drawtext "@24,BOLD,color 150,courier@O" AT 200,90
  417. drawtext "@12,BOLD,color 213,times@N" AT 220,95
  418. drawtext "@24,BOLD,color 100,helvetica@G" AT 230,100
  419. drawtext "@12,color 70,Times@by Scott Mathews" AT 118,150
  420.  
  421. rem big gong sound here
  422. Sound "TADAA"
  423. position 13,12
  424. textcolor 6
  425. print "Would you like"
  426. position 10,13
  427. input "some thrilling info? ", answer$
  428. if left$(answer$,1) = "y" then
  429. cls
  430. color 21
  431. fillrect 0,0 to 319,239
  432. position 10,0
  433. print "You know the score."
  434. position 11,1
  435. print "First to ";victory;" wins."
  436. position 7,2
  437. print "Here's the keyboard setup:"
  438. textcolor lpadcolor
  439. position 0,4
  440. print "Left Player:"
  441. position 0,5
  442. print "paddle up:      ";Lup$
  443. position 0,6
  444. print "paddle down:    ";Ldown$
  445. position 0,7
  446. print "add ball speed: ";Lspeed$
  447. position 0,8
  448. print "add english:    ";Lenglish$
  449. textcolor rpadcolor
  450. position 19,4
  451. print "Right Player:"
  452. position 19,5
  453. print "paddle up:      ";Rup$
  454. position 19,6
  455. print "paddle down:    ";Rdown$
  456. position 19,7
  457. print "add ball speed: ";Rspeed$
  458. position 19,8
  459. print "add english:    ";Renglish$
  460. textcolor 100
  461. position 15,11
  462. print "CAUTION:"
  463. position 7,12
  464. print "english does wacky things"
  465. textcolor 6
  466. position 9,14
  467. print "Press any key to play"
  468. while inkey$ <> ""
  469. wend
  470. while inkey$ = ""
  471. wend
  472. endif
  473. cls
  474. return
  475.  
  476. getnames:
  477.  
  478. color 21
  479. fillrect 0,0 to 319,239
  480. position 0,3
  481. textcolor lpadcolor
  482. print " Left Player," 
  483. input " what is your name?  ", name1$
  484. position 0,8
  485. textcolor rpadcolor
  486. print " Right player," 
  487. input " what is your name?  ",name2$
  488. position 18,12
  489. textcolor 100
  490. print "Here ";
  491. sleep 5
  492. position 19,13
  493. print "we"
  494. sleep 5
  495. position 19,14
  496. print "go"
  497. sleep 5
  498. cls
  499. return
  500.  
  501. playagain:
  502. position 5,12
  503. print "Would you like to play again? " 
  504. input "                 ",answer$
  505. if left$(answer$,1) = "y" then
  506. leftscore = 0
  507. rightscore = 0
  508. again = true
  509. inplay = true
  510. cls
  511. endif
  512.  
  513. return
  514.  
  515.  
  516.  
  517.